package com.briup.mt.web.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;


import com.briup.mt.Api.AipImageClient;
import com.briup.mt.bean.CallLog;
import com.briup.mt.service.ICallLogService;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;

import com.baidu.aip.imageclassify.AipImageClassify;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(description = "图像识别相关接口")
@Validated
@RestController
@RequestMapping("/recognition")
public class RecognitionController {
	@Autowired
	private ICallLogService callLogService;

	@ApiOperation(value = "图片识别")
	@PostMapping("image")
	public void image(@RequestParam("file") MultipartFile file, HttpServletResponse resp) throws Exception {
		// 获取图像识别客户端
		AipImageClassify client = AipImageClient.getInstance();
		// 调用sdk完成识别功能
		JSONObject res = client.advancedGeneral(file.getBytes(), new HashMap<>());
		// 定义响应报文响应头信息
		resp.setHeader("Content-Type", "application/json;charset=UTF-8");


		CallLog log = new CallLog();
		log.setCallTime(new Date().getTime());
		log.setComment(res.toString());
		callLogService.save(log);

		// 将识别结果写入到响应体
		resp.getWriter().write(res.toString());

	}
}
